package com.cty._03_SolutionSeekingMindset._30_MinInStack;

import java.util.Stack;

/**
 * @Auther: cty
 * @Date: 2020/7/11 8:59
 * @Description: 面试题30：包含min函数的栈
 * 题目：定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的min
 * 函数。在该栈中，调用min、push及pop的时间复杂度都是O(1)。
 * @version: 1.0
 */
public class StackWithMin {

    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public StackWithMin(){
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public boolean isEmpty(){
        return stack.isEmpty() || minStack.isEmpty();
    }

    public int size(){
        return stack.size();
    }

    public void push(int value){
        stack.push(value);
        if(minStack.size()==0 || value<minStack.peek())
            minStack.push(value);
        else
            minStack.push(minStack.peek());
    }

    public int pop(){
        if(isEmpty())
            return -999999;

        minStack.pop();
        return stack.pop();
    }

    public int peek(){
        if(isEmpty())
            return -999999;
        return stack.peek();
    }

    public int min(){
        if(isEmpty())
            return -999999;

        return minStack.peek();
    }
}  // end class
